package net.thermetics.fx.experimental;

import javax.servlet.http.HttpServlet;
/*
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.thermetics.fx.util.GAEConnectionManager;
import net.thermetics.fx.util.MD5;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HttpContext;
*/

@SuppressWarnings("serial")
public class VBLogin extends HttpServlet {
	/*
	private String securityToken;
	private DefaultHttpClient client;
	private String user;
	private String password;
	private String md5pw;

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/html");
		user = req.getParameter("user");
		password = req.getParameter("password");
		md5pw = md5(password);
		
		client = new DefaultHttpClient(new GAEConnectionManager(), new BasicHttpParams());
		client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
		
		System.out.println("Step 1");
		step1();
		System.out.println("Cookies after step 1:");
		for (Cookie cookie : client.getCookieStore().getCookies()) {
			System.out.println(cookie);
		}
		
		System.out.println("Step 2");
		step2();
		System.out.println("Cookies after step 2:");
		for (Cookie cookie : client.getCookieStore().getCookies()) {
			System.out.println(cookie);
		}
		
		System.out.println("Step 3");
		step3(resp.getOutputStream());
		System.out.println("Done");
	}
	
	private void step1() throws IOException {
		//Connect to index.php for initial cookies and security token.
		HttpGet get = new HttpGet("http://www.christianforums.com/index.php");
		get.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7");
		HttpResponse resp = client.execute(get);
		
		HttpEntity ent2 = resp.getEntity();
		System.out.println("   response: " + resp.getStatusLine());
		InputStreamReader reader = new InputStreamReader(ent2.getContent());
		BufferedReader r = new BufferedReader(reader);
		String line = "";
		
		while ((line = r.readLine()) != null) {
			if (line.startsWith("var SECURITYTOKEN = ")) {
				securityToken = line.substring(line.indexOf("= \"") + 3);
				securityToken = securityToken.substring(0, securityToken.length() - 2);
				break;
			}
		}
		
		r.close();
		ent2.consumeContent();
	}
	
	private void step2() throws IOException {
		HttpPost post = new HttpPost("http://www.christianforums.com/login.php?do=login");
		System.out.println("hello from loginform");
		post.addHeader("Referer", "http://www.christianforums.com");
		post.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7");
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("securitytoken", securityToken));
		params.add(new BasicNameValuePair("do", "login"));
		params.add(new BasicNameValuePair("s", ""));
		params.add(new BasicNameValuePair("vb_login_username", user));
		params.add(new BasicNameValuePair("vb_login_password", md5pw));
		params.add(new BasicNameValuePair("vb_login_md5password", md5pw));
		params.add(new BasicNameValuePair("forceredirect", "1"));
		params.add(new BasicNameValuePair("cookieuser", "1"));
		
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
		post.setEntity(entity);

		
		HttpResponse response = client.execute(post);
		System.out.println("   response: " + response.getStatusLine());
		HttpEntity ent = response.getEntity();	
		
		if (ent != null) {
			ent.consumeContent();
		}
	}
	
	private void step3(ServletOutputStream out) throws IOException {
		HttpGet get = new HttpGet("http://www.christianforums.com/index.php");
		get.addHeader("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7");
		get.addHeader("Referer", "http://www.christianforums.com");
		HttpResponse resp = client.execute(get);
		
		System.out.println("   response: " + resp.getStatusLine());
		HttpEntity ent2 = resp.getEntity();
		InputStreamReader reader = new InputStreamReader(ent2.getContent());
		BufferedReader r = new BufferedReader(reader);
		String line = "";
		
		while ((line = r.readLine()) != null) {
			out.println(line);
		}
		
		r.close();
		ent2.consumeContent();	
	}
	
	private String md5(String password) {
		MD5 md5;
		try {
			md5 = MD5.getInstance();
			return md5.hashData(password.getBytes());
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	*/
	
}
